
/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
 * CANN Open Software License Agreement Version 2.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */
#include "utils/assert.h"
#include "log/log.h"
#include "mki/utils/platform/platform_info.h"
#include "asum.h"
#include "common_tiling.h"
#include "sasum_tiling.h"

constexpr uint32_t DEFAULT_VECTOR_NUM = 40;
constexpr uint64_t WORKSPACE_SIZE = 16777216; // 16 * 1024 * 1024

namespace AsdSip {
using namespace Mki;

AsdSip::AspbStatus SasumTiling(const LaunchParam &launchParam, KernelInfo &kernelInfo)
{
    void *tilingData = kernelInfo.GetTilingHostAddr();

    CommonTilingData *tilingDataPtr = reinterpret_cast<CommonTilingData *>(tilingData);

    ASDSIP_CHECK(tilingData != nullptr, "tilingDataPtr should not be empty",
              return AsdSip::ErrorType::ACL_ERROR_INVALID_PARAM);

    uint32_t vecCoreNum = PlatformInfo::Instance().GetCoreNum(CoreType::CORE_TYPE_VECTOR);
    vecCoreNum = vecCoreNum > DEFAULT_VECTOR_NUM ? DEFAULT_VECTOR_NUM : vecCoreNum;
    if (vecCoreNum == 0) {
        vecCoreNum = 1;
    }
    OpParam::Asum param = AnyCast<OpParam::Asum>(launchParam.GetParam());
    uint32_t useCoreNum = ConfigCommonTilingData(tilingDataPtr, param.n, vecCoreNum);
    if (useCoreNum == 0) {
        useCoreNum = 1;
    }

    kernelInfo.SetBlockDim(useCoreNum);
    kernelInfo.GetScratchSizes() = {WORKSPACE_SIZE};
    ASDSIP_LOG(DEBUG) << "KernelInfo:\n" << kernelInfo.ToString();

    return AsdSip::ErrorType::ACL_SUCCESS;
}
}